package com.itboye.luntan.web;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import com.itboye.luntan.web.CustomWebView.LongClickCallBack;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsunhome.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class LuntanWebView extends BaseActivity implements LongClickCallBack{
	
	
	
	private CustomWebView mCustomWebView;
	private CustomDialog mCustomDialog;
	private ArrayAdapter<String> adapter;
	private boolean isQR;//判断是否为二维码
//	private Result result;//二维码解析结果
	
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if (msg.what == 0){
				if (isQR){
		        	 adapter.add("识别图中二维码");   
		        }
				adapter.notifyDataSetChanged();
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.activity_main);
		
		setStatusBarColor(R.color.home_blue);
		initWebView();
	}

	private void initWebView() {
		// 初始WebView化控件
		mCustomWebView = new CustomWebView(this, this);
		mCustomWebView.loadUrl(NetPublicConstant.WEB_URL+"/Post/viewPost?id=456");//加载页面
		mCustomWebView.setFocusable(true);
		mCustomWebView.setFocusableInTouchMode(true);
		addContentView(mCustomWebView, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
	}

	@Override
	public void onLongClickCallBack(final String imgUrl) {
		// 获取到图片地址后做相应的处理
		new Thread(){
    		public void run() {
//    			decodeImage(imgUrl);
    			
    			System.out.println("LLLLLLLLLLLLLLLLLLL"+imgUrl);
    			handler.sendEmptyMessage(0);
    		};
    	}.start();
    	
    	showDialog();
	}
	
/*	*//**
     * 判断是否为二维码
     * @param url 图片地址
     * @return
     *//*
    private boolean decodeImage(String sUrl){
    	result = DecodeImage.handleQRCodeFormBitmap(getBitmap(sUrl));
		if(result == null){
			isQR = false;
        }else {
        	isQR = true;
        }
        return isQR;
    }
    */
    /**
     * 根据地址获取网络图片
     * @param sUrl 图片地址
     * @return
     * @throws IOException
     */
    public static Bitmap getBitmap(String sUrl){
		try {
			URL url = new URL(sUrl);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	        conn.setConnectTimeout(5000);
	        conn.setRequestMethod("GET");
	        if(conn.getResponseCode() == 200){
		        InputStream inputStream = conn.getInputStream();
		        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
		        return bitmap;
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
   }
    
    /**
     * 显示Dialog
     */
    private void  showDialog() {
    	
    	adapter = new ArrayAdapter<String>(this,R.layout.item_dialog);  
        adapter.add("发送给朋友");  
        adapter.add("保存到手机");  
        adapter.add("收藏");
    	
    	mCustomDialog = new CustomDialog(this, R.layout.custom_dialog) {
			
			@Override
			public void initViews() {
				// 初始CustomDialog化控件
				ListView mListView = (ListView) findViewById(R.id.lv_dialog);
		        mListView.setAdapter(adapter); 
		        mListView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						// 点击事件
						switch (position) {
						case 0:
							Toast.makeText(LuntanWebView.this, "已发送给朋友", Toast.LENGTH_LONG).show();
							closeDialog();
							break;
						case 1:
							Toast.makeText(LuntanWebView.this, "已保存到手机", Toast.LENGTH_LONG).show();
							closeDialog();
							break;
						case 2:
							Toast.makeText(LuntanWebView.this, "已收藏", Toast.LENGTH_LONG).show();
							closeDialog();
							break;
						case 3:
//							Toast.makeText(MainActivity.this, "二维码识别结果: " + result.toString(), Toast.LENGTH_LONG).show();		
							closeDialog();
							break;
						}
						
					}
				});
			}
		};
		mCustomDialog.show();
    }
    
    
}
